package com.portal4kids.controller;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.portal4kids.model.People;
import com.portal4kids.model.ServicesAdditional;
import com.portal4kids.model.ServicesArt;
import com.portal4kids.model.ServicesLanguage;
import com.portal4kids.model.ServicesMusic;
import com.portal4kids.model.ServicesSport;
import com.portal4kids.model.ServicesTheaterScout;
import com.portal4kids.util.HibernateUtil;

public class ServicesManager extends HibernateUtil{
	
	public ServicesSport saveSport(ServicesSport sport){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		System.out.println("Sport: " + sport);
		sport.setAvarage(0);
		sport.setNumberOfVotes(0);
		sport.setRating(0);
		session.save(sport);
		session.getTransaction().commit();
		return sport;
		
	}
	
	public ServicesArt saveArt(ServicesArt art){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		System.out.println("Art: " + art);
		art.setAvarage(0);
		art.setNumberOfVotes(0);
		art.setRating(0);
		session.save(art);
		session.getTransaction().commit();
		return art;
		
	}
	
	public ServicesLanguage saveLanguage(ServicesLanguage language){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		System.out.println("Language: " + language);
		language.setAvarage(0);
		language.setNumberOfVotes(0);
		language.setRating(0);
		session.save(language);
		session.getTransaction().commit();
		return language;
		
	}
	
	public ServicesMusic saveMusic(ServicesMusic music){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		System.out.println("Music: " + music);
		music.setAvarage(0);
		music.setNumberOfVotes(0);
		music.setRating(0);
		session.save(music);
		session.getTransaction().commit();
		return music;
	}
	
	public ServicesTheaterScout saveT_S(ServicesTheaterScout t_s){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		System.out.println("T_S: " + t_s);
		session.save(t_s);
		session.getTransaction().commit();
		return t_s;
	}
	
	public ServicesAdditional saveAdditionalService(ServicesAdditional additional){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		System.out.println("Additional: " + additional);
		session.save(additional);
		session.getTransaction().commit();
		return additional;
		
	}
	
	 public List<ServicesSport> getAllSportFromDB(){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<ServicesSport> sportList = null;
		Query q = session.createQuery("from ServicesSport");
		sportList = q.list();
		session.getTransaction().commit();
		return sportList;
	 }
	
	public List<ServicesSport> getSportFromDB(ServicesSport sport ){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<ServicesSport> sportList = null;
		
		if("Dowolna".equals(sport.getAddress())){
			Query q = session.createQuery("from ServicesSport where age<=" + sport.getAge() + " and time>="+ sport.getTime() + " and typeOfSport='"+ sport.getTypeOfSport() + "' ORDER BY(avarage) DESC");
			sportList = q.list();
		}else{
			Query q = session.createQuery("from ServicesSport where age<=" + sport.getAge() + " and address='" + sport.getAddress() + "' and time>="+ sport.getTime() + " and typeOfSport='"+ sport.getTypeOfSport() + "' ORDER BY(avarage) DESC");
			sportList = q.list();
		}
		
		
		List<ServicesSport> finalList = new ArrayList<ServicesSport>();
				
		if(!sportList.isEmpty() && sport.getParents().equals("TAK")){
			for (ServicesSport servicesSport : sportList) {
				
				if(servicesSport.getParents().equals("TAK")){
					finalList.add(servicesSport);
				}
			}
		}else{
			finalList = sportList;
		}
		
		
		session.getTransaction().commit();
		return finalList;
		
	}
	
	public List<ServicesSport> getSportAfterId(int id ){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<ServicesSport> sportList = null;
		Query q = session.createQuery("from ServicesSport where id_sport="+ id);
		sportList = q.list();
		session.getTransaction().commit();
		return sportList;
	}
	
	public List<ServicesLanguage> getLanguageFromDB(ServicesLanguage language){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<ServicesLanguage> languageList = new ArrayList<ServicesLanguage>();
		
		if("Dowolna".equals(language.getAddress())){
			if((language.getIndividual().equals("NIE") && language.getGroups().equals("NIE")) && (language.getAtHome().equals("NIE") && language.getAtSchool().equals("NIE"))){
				Query q = session.createQuery("from ServicesLanguage where time>="+ language.getTime() + " and methodLearning='" + language.getMethodLearning() + "' ORDER BY(avarage) DESC");
				languageList = q.list();
			}else if(language.getIndividual().equals("NIE") && language.getGroups().equals("NIE")){
				Query q = session.createQuery("from ServicesLanguage where time>="+ language.getTime() + " and methodLearning='" + language.getMethodLearning() + "' and atHome='" + language.getAtHome() + "' and atSchool='" + language.getAtSchool() + "' ORDER BY(avarage) DESC");
				languageList = q.list();
			}else if(language.getAtHome().equals("NIE") && language.getAtSchool().equals("NIE")){
				Query q = session.createQuery("from ServicesLanguage where time>="+ language.getTime() + " and methodLearning='" + language.getMethodLearning() + "' and individual='" + language.getIndividual() + "' and groups='" + language.getGroups() + "' ORDER BY(avarage) DESC");
				languageList = q.list();
			}else{
				Query q = session.createQuery("from ServicesLanguage where time>="+ language.getTime() + " and methodLearning='" + language.getMethodLearning() + "' and atHome='" + language.getAtHome() + "' and atSchool='" + language.getAtSchool() + "' and individual='" + language.getIndividual() + "' and groups='" + language.getGroups() + "' ORDER BY(avarage) DESC");
				languageList = q.list();
			}
		}else{
			if((language.getIndividual().equals("NIE") && language.getGroups().equals("NIE")) && (language.getAtHome().equals("NIE") && language.getAtSchool().equals("NIE"))){
				Query q = session.createQuery("from ServicesLanguage where address='" + language.getAddress() + "'and time>="+ language.getTime() + " and methodLearning='" + language.getMethodLearning() + "'");
				languageList = q.list();
			}else if(language.getIndividual().equals("NIE") && language.getGroups().equals("NIE")){
				Query q = session.createQuery("from ServicesLanguage where address='" + language.getAddress() + "' and time>="+ language.getTime() + " and methodLearning='" + language.getMethodLearning() + "' and atHome='" + language.getAtHome() + "' and atSchool='" + language.getAtSchool() + "' ORDER BY(avarage) DESC");
				languageList = q.list();
			}else if(language.getAtHome().equals("NIE") && language.getAtSchool().equals("NIE")){
				Query q = session.createQuery("from ServicesLanguage where address='" + language.getAddress() + "' and time>="+ language.getTime() + " and methodLearning='" + language.getMethodLearning() + "' and individual='" + language.getIndividual() + "' and groups='" + language.getGroups() + "' ORDER BY(avarage) DESC");
				languageList = q.list();
			}else{
				Query q = session.createQuery("from ServicesLanguage where address='" + language.getAddress() + "' and time>="+ language.getTime() + " and methodLearning='" + language.getMethodLearning() + "' and atHome='" + language.getAtHome() + "' and atSchool='" + language.getAtSchool() + "' and individual='" + language.getIndividual() + "' and groups='" + language.getGroups() + "' ORDER BY(avarage) DESC");
				languageList = q.list();
			}
		}
		
		session.getTransaction().commit();
		return languageList;
	}
	
	public List<ServicesLanguage> getLanguageDetails(int id){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<ServicesLanguage> languageList = new ArrayList<ServicesLanguage>();
		Query q = session.createQuery("from ServicesLanguage where id_language="+id);
		languageList = q.list();
		session.getTransaction().commit();
		return languageList;
		
	}
	
	public List<ServicesArt> getArtFromDB(ServicesArt art){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<ServicesArt> artList = new ArrayList<ServicesArt>();
		if("Dowolna".equals(art.getAddress())){
			Query q = session.createQuery("from ServicesArt where age<=" + art.getAge() + " and time>=" + art.getTime() + " ORDER BY(avarage) DESC");
			artList = q.list();
		}else{
			Query q = session.createQuery("from ServicesArt where age<=" + art.getAge() + " and time>=" + art.getTime() + " and address='" + art.getAddress() + "' ORDER BY(avarage) DESC");
			artList = q.list();
		}
		
		session.getTransaction().commit();
		return artList;
		
	}
	
	public List<ServicesArt> getArtDetails(int id){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<ServicesArt> artList = new ArrayList<ServicesArt>();

		Query q = session.createQuery("from ServicesArt where id_art=" + id);
		artList = q.list();
		
		session.getTransaction().commit();
		return artList;
	}
	
	public List<ServicesMusic> getMusicFromDB(ServicesMusic music){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<ServicesMusic> musicList = new ArrayList<ServicesMusic>();
		if("Dowolna".equals(music.getAddress())){
			Query q = session.createQuery("from ServicesMusic where type='" + music.getType() + "' and time>=" + music.getTime() + " ORDER BY(avarage) DESC");
			musicList = q.list();
		}else{
			Query q = session.createQuery("from ServicesMusic where address='" + music.getAddress() + "' and type='" + music.getType() + "' and time>=" + music.getTime() + " ORDER BY(avarage) DESC");
			musicList = q.list();
		}
		session.getTransaction().commit();
		return musicList;
	}
	
	public List<ServicesMusic> getMusicDetails(int id){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<ServicesMusic> musicList = new ArrayList<ServicesMusic>();
		
		Query q = session.createQuery("from ServicesMusic where id_music="+id );
		musicList = q.list();
		
		session.getTransaction().commit();
		return musicList;
	}
	
	public List<ServicesTheaterScout> getT_SFromDB(ServicesTheaterScout t_s){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<ServicesTheaterScout> t_sList = new ArrayList<ServicesTheaterScout>();
		if("Dowolna".equals(t_s.getAddress())){
			Query q = session.createQuery("from ServicesTheaterScout where typeOfService='" + t_s.getTypeOfService() + "' and time>=" + t_s.getTime() + " ORDER BY(avarage) DESC");
			t_sList = q.list();
		}else{
			Query q = session.createQuery("from ServicesTheaterScout where typeOfService='" + t_s.getTypeOfService() + "' and address='" + t_s.getAddress() + "' and time>=" + t_s.getTime() + " ORDER BY(avarage) DESC");
			t_sList = q.list();
		}
		session.getTransaction().commit();
		return t_sList;
		
	}
	
	public List<ServicesTheaterScout> getT_SDetails(int id){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<ServicesTheaterScout> t_sList = new ArrayList<ServicesTheaterScout>();
		
		Query q = session.createQuery("from ServicesTheaterScout where id_service="+id);
		t_sList = q.list();
		
		session.getTransaction().commit();
		return t_sList;
	}
	
	public List<ServicesAdditional> getAdditionalFromDB(ServicesAdditional additional){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<ServicesAdditional> additionalList = new ArrayList<ServicesAdditional>();
		if("Dowolna".equals(additional.getAddress())){
			Query q = session.createQuery("from ServicesAdditional where type='" + additional.getType() + "' ORDER BY(avarage) DESC");
			additionalList = q.list();
		}else{
			Query q = session.createQuery("from ServicesAdditional where type='" + additional.getType() + "' and address='" + additional.getAddress() + "' ORDER BY(avarage) DESC");
			additionalList = q.list();
		}
		
		session.getTransaction().commit();
		return additionalList;
	}
	
	public List<ServicesAdditional> getAdditionalDetails(int id){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<ServicesAdditional> additionalList = new ArrayList<ServicesAdditional>();

		Query q = session.createQuery("from ServicesAdditional where id_service="+id );
		additionalList = q.list();
		
		session.getTransaction().commit();
		return additionalList;
	}
	public static void addRankSport(int id, int rank){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		ServicesSport sport = (ServicesSport) session.load(ServicesSport.class, id);
		
		int numberOfVotes = sport.getNumberOfVotes();
		numberOfVotes++;
		sport.setNumberOfVotes(numberOfVotes);
		
		int currentRank = sport.getRating();
		currentRank += rank;
		sport.setRating(currentRank);
		if(numberOfVotes == 0){
			System.out.println("0000000000000000000000");
		}else{
		int avarage = currentRank / numberOfVotes;
		sport.setAvarage(avarage);
		}
		session.update(sport);
		session.getTransaction().commit();

	}
	
	public static void addRankLanguage(int id, int rank){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		ServicesLanguage language = (ServicesLanguage) session.load(ServicesLanguage.class, id);
		
		int numberOfVotes = language.getNumberOfVotes();
		numberOfVotes++;
		language.setNumberOfVotes(numberOfVotes);
		
		int currentRank = language.getRating();
		currentRank += rank;
		language.setRating(currentRank);
		if(numberOfVotes == 0){
			System.out.println("0000000000000000000000");
		}else{
		int avarage = currentRank / numberOfVotes;
		language.setAvarage(avarage);
		}
		session.update(language);
		session.getTransaction().commit();

	}
	
	public static void addRankArt(int id, int rank){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		ServicesArt art = (ServicesArt) session.load(ServicesArt.class, id);
		
		int numberOfVotes = art.getNumberOfVotes();
		numberOfVotes++;
		art.setNumberOfVotes(numberOfVotes);
		
		int currentRank = art.getRating();
		currentRank += rank;
		art.setRating(currentRank);
		if(numberOfVotes == 0){
			System.out.println("0000000000000000000000");
		}else{
		int avarage = currentRank / numberOfVotes;
		art.setAvarage(avarage);
		}
		session.update(art);
		session.getTransaction().commit();

	}
	
	public static void addRankMusic(int id, int rank){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		ServicesMusic music = (ServicesMusic) session.load(ServicesMusic.class, id);
		
		int numberOfVotes = music.getNumberOfVotes();
		numberOfVotes++;
		music.setNumberOfVotes(numberOfVotes);
		
		int currentRank = music.getRating();
		currentRank += rank;
		music.setRating(currentRank);
		if(numberOfVotes == 0){
			System.out.println("0000000000000000000000");
		}else{
		int avarage = currentRank / numberOfVotes;
		music.setAvarage(avarage);
		}
		session.update(music);
		session.getTransaction().commit();

	}
	
	public static void addRankT_S(int id, int rank){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		ServicesTheaterScout t_s = (ServicesTheaterScout) session.load(ServicesTheaterScout.class, id);
		
		int numberOfVotes = t_s.getNumberOfVotes();
		numberOfVotes++;
		t_s.setNumberOfVotes(numberOfVotes);
		
		int currentRank = t_s.getRating();
		currentRank += rank;
		t_s.setRating(currentRank);
		if(numberOfVotes == 0){
			System.out.println("0000000000000000000000");
		}else{
		int avarage = currentRank / numberOfVotes;
		t_s.setAvarage(avarage);
		}
		session.update(t_s);
		session.getTransaction().commit();

	}
	
	public static void addRankAdditional(int id, int rank){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		ServicesAdditional additional = (ServicesAdditional) session.load(ServicesAdditional.class, id);
		
		int numberOfVotes = additional.getNumberOfVotes();
		numberOfVotes++;
		additional.setNumberOfVotes(numberOfVotes);
		
		int currentRank = additional.getRating();
		currentRank += rank;
		additional.setRating(currentRank);
		if(numberOfVotes == 0){
			System.out.println("0000000000000000000000");
		}else{
		int avarage = currentRank / numberOfVotes;
		additional.setAvarage(avarage);
		}
		session.update(additional);
		session.getTransaction().commit();

	}

}
